package com.woshua.vptabmodel.transformer;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * @author Steven.zhao
 * email:hongtu.zhao@goodwinsoft.net
 * date:2019/7/20
 * desc:
 */
public class RotateTransformer implements ViewPager.PageTransformer {

    private static final int MAX_ROTATE=15;

    //a->b
//        a:pos:(0,-1)
//        b:pos(1,0)
//        b->a
//        a:pos:(-1,0)
//        b:pos(0,1)

    @Override
    public void transformPage(@NonNull View view, float progress) {
        if(progress<-1)
        {
            view.setPivotY(view.getHeight());
            view.setPivotX(view.getWidth());
            view.setRotation(-MAX_ROTATE);
        }
//          [-1,1]
        else if(progress<=1)
        {
//              左边的页面
            if(progress<0)
            {
               //a:pos:(0,-1)
                view.setPivotY(view.getHeight());
                //0.5w->w
                view.setPivotX(view.getWidth()*0.5f+(-progress)*view.getWidth()*0.5f);
                //(0,-1)  0->-MAX
                view.setRotation(MAX_ROTATE*progress);
            }
            else
            {
                //右边界面
                //        b:pos(1,0)
                view.setPivotY(view.getHeight());
                // 0w->0.5w
                view.setPivotX((1-progress)*view.getWidth()*0.5f);
                //max ->0
                view.setRotation(MAX_ROTATE*progress);
            }
        }
//          (1,]
        else
        {
            view.setPivotY(view.getHeight());
            view.setPivotX(0);
            view.setRotation(MAX_ROTATE);
        }
    }
}
